import 'package:dio/dio.dart';
import 'package:loxen/api/api.dart';
import 'package:loxen/api/http_util.dart';
import 'package:loxen/viewmode/base_view_model.dart';

class CallBack {
  final void Function() onSuccess;
  final void Function(Exception error) onError;

  const CallBack(this.onSuccess, this.onError);
}

class AttestationCompanyRequest extends BaseViewModel {
  CallBack _callBack;

  set callBack(CallBack value) {
    _callBack = value;
  }

  void business(FormData portrait,
      {void Function() onSuccess, void Function(String error) onFail}) async{

    ApiService.business(portrait).listen((event) {
      if (event.statusCode == 200) {
        if (onSuccess != null) onSuccess.call();
      } else {
        _callBack.onError(Exception('未知异常'));
        if (onFail != null) onFail.call('未知异常');
      }
    }, onError: (error) {
      if (onFail != null) {
        if (error is ServiceException) {
          var data = error.userMessage;
          if (data is Map) {
            onFail.call(data["data"].toString());
          }
        }else{
          onFail.call(error.toString());
        }
      }
    });
  }
}
